package com.itboye.jigongbao.widget.titlebarwidget

import android.animation.ObjectAnimator
import android.content.Context
import android.graphics.Color
import android.support.annotation.ColorInt
import android.support.v4.graphics.ColorUtils
import android.util.AttributeSet
import android.util.Log
import android.view.MotionEvent
import android.view.View
import android.view.WindowManager
import android.widget.FrameLayout
import android.widget.ScrollView
import com.itboye.jigongbao.R
import com.itboye.jigongbao.utils.SizeUtils

/**
 * Created by Administrator on 2017/11/28.
 */
class TranslucentScrollView : ScrollView {

    //伸缩视图
    private var zoomView: View? = null
    //伸缩视图初始高度
    private var zoomViewInitHeight = 0
    // 记录首次按下位置
    private var mFirstPosition = 0f
    // 是否正在放大
    private var mScaling: Boolean? = false

    //渐变的视图
    private var transView: View? = null
    //渐变颜色
    private var transColor = Color.WHITE
    //渐变开始位置
    private var transStartY = 50
    //渐变结束位置
    private var transEndY = 300

    //渐变开始默认位置，Y轴，50dp
    private val DFT_TRANSSTARTY = 50
    //渐变结束默认位置，Y轴，300dp
    private val DFT_TRANSENDY = 300

    private var translucentChangedListener: TranslucentChangedListener? = null

    /**
     * 获取透明度
     *
     * @return
     */
    private val transAlpha: Int
        get() {
            val scrollY = scrollY.toFloat()
            return if (transStartY != 0) {
                if (scrollY <= transStartY) {
                    0
                } else if (scrollY >= transEndY) {
                    255
                } else {
                    ((scrollY - transStartY) / (transEndY - transStartY) * 255).toInt()
                }
            } else {
                if (scrollY >= transEndY) {
                    255
                } else ((transEndY - scrollY) / transEndY * 255).toInt()
            }
        }

    interface TranslucentChangedListener {
        /**
         * 透明度变化，取值范围0-255
         *
         * @param transAlpha
         */
        fun onTranslucentChanged(transAlpha: Int)
    }

    constructor(context: Context) : super(context) {}

    constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {}

    constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {}

    fun setTranslucentChangedListener(translucentChangedListener: TranslucentChangedListener) {
        this.translucentChangedListener = translucentChangedListener
    }

    /**
     * 设置伸缩视图
     *
     * @param zoomView
     */
    fun setPullZoomView(zoomView: View) {
        this.zoomView = zoomView
        zoomViewInitHeight = zoomView.layoutParams.height
        if (zoomViewInitHeight == FrameLayout.LayoutParams.MATCH_PARENT || zoomViewInitHeight == WindowManager.LayoutParams.WRAP_CONTENT) {
            zoomView.post { zoomViewInitHeight = this@TranslucentScrollView.zoomView!!.height }
        }
    }

    /**
     * 设置渐变视图
     *
     * @param transView  渐变的视图
     * @param transColor 渐变颜色
     * @param transEndY  渐变结束位置
     */
    @JvmOverloads
    fun setTransView(transView: View, @ColorInt transColor: Int = resources.getColor(R.color.main_blue), transStartY: Int = SizeUtils.dip2px(context, DFT_TRANSSTARTY.toFloat()), transEndY: Int = SizeUtils.dip2px(context, DFT_TRANSENDY.toFloat())) {
        this.transView = transView
        //初始视图-透明
        this.transView!!.setBackgroundColor(ColorUtils.setAlphaComponent(transColor, 0))
        this.transStartY = transStartY
        this.transEndY = transEndY
        this.transColor = transColor
        if (transStartY > transEndY) {
            throw IllegalArgumentException("transStartY 不得大于 transEndY .. ")
        }
    }

    /**
     * 重置ZoomView
     */
    private fun resetZoomView() {
        val lp = zoomView!!.layoutParams
        val h = zoomView!!.layoutParams.height.toFloat()// ZoomView当前高度

        // 设置动画
        val anim = ObjectAnimator.ofFloat(0.0f, 1.0f).setDuration(200)

        anim.addUpdateListener { animation ->
            val cVal = animation.animatedValue as Float
            lp.height = (h - (h - zoomViewInitHeight) * cVal).toInt()
            zoomView!!.layoutParams = lp
        }
        anim.start()
    }

    override fun onScrollChanged(l: Int, t: Int, oldl: Int, oldt: Int) {
        super.onScrollChanged(l, t, oldl, oldt)
        val transAlpha = transAlpha

        if (transView != null) {
            Log.d(TAG, "[onScrollChanged .. in ], 透明度 == " + transAlpha)
            transView!!.setBackgroundColor(ColorUtils.setAlphaComponent(transColor, transAlpha))
        }
        if (translucentChangedListener != null) {
            translucentChangedListener!!.onTranslucentChanged(transAlpha)
        }
    }

    override fun onTouchEvent(event: MotionEvent): Boolean {
        if (zoomView != null) {
            val params = zoomView!!.layoutParams
            when (event.action) {
                MotionEvent.ACTION_UP -> {
                    //手指离开后恢复图片
                    mScaling = false
                    resetZoomView()
                }
                MotionEvent.ACTION_MOVE -> {
                    if ((!mScaling!!)!!) {
                        if (scrollY == 0) {
                            mFirstPosition = event.y
                        }
                        else {
//                            break
                        }
                    }

                    val distance = ((event.y - mFirstPosition) * 0.6).toInt()
                    if (distance < 0) {
//                        break
                    }
                    mScaling = true
                    params.height = zoomViewInitHeight + distance

                    Log.d(TAG, "params.height == " + params.height + ", zoomViewInitHeight == " + zoomViewInitHeight + ", distance == " + distance)
                    zoomView!!.layoutParams = params
                    return true
                }
            }
        }

        return super.onTouchEvent(event)
    }

    companion object {

        internal val TAG = "TranslucentScrollView"
    }

}
/**
 * 设置渐变视图
 *
 * @param transView 渐变的视图
 */